package hot100;

public class FindMin {
    public int findMin(int[] nums) {
        int n = nums.length;
        int left = 0;
        int right = n - 1;

        if (nums[0] <= nums[n - 1]) {
            //本身就是有序的
            return nums[0];
        }

        while (left <= right) {
            int mid = (left + right) / 2;
            if (mid > 0 && nums[mid] < nums[mid - 1]) {
                return nums[mid];
            }

            if (nums[0] <= nums[mid]) {
                //左边有序，向右找
                left = mid + 1;
            } else {
                //右边有序，向左找
                right = mid - 1;
            }
        }

        return nums[0];
    }
}
